package mk.edu.ii.navigatemacedonia;

import java.util.List;

import org.openintents.hardware.SensorManagerSimulator;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.MapView.LayoutParams;

public class MapPreviewActivity extends MapActivity {
	private MapView mapView;
	List<Overlay> listOfOverlays ;
	float results[]=new float[3];
	CompassOverlay compassOverlay;
	private SensorManagerSimulator mSensorManager;
	/** Called when the activity is first created. 
	 * 
	 */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.mappreviewactivity);
				
		mapView = (MapView)findViewById(R.id.map_view);
		compassOverlay = new CompassOverlay(this,mapView);
		
		listOfOverlays = mapView.getOverlays();
		mapView.setStreetView(false);
		mapView.setSatellite(true); 
		mapView.setTraffic(false);
		
		LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
		ZoomControls mZoom =(ZoomControls) mapView.getZoomControls(); 
 
        zoomLayout.addView(mZoom, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        
        mapView.displayZoomControls(true);
      
       goToMyLocation();       
        
	}
	
	/** Called when the Menu is created.
	 * 
	 */
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	
    	SubMenu mapMode = menu.addSubMenu("MapMode");
    	SubMenu compass = menu.addSubMenu("Compass");
    	
    	mapMode.add(1, 1, Menu.NONE, "Street View");
    	mapMode.add(1, 2, Menu.NONE, "Satellite");
    	mapMode.add(1, 3, Menu.NONE, "Traffic");
    	mapMode.setGroupCheckable(1, true, true);
    	
    	compass.add(2, 4, Menu.NONE, "ON");
    	compass.add(2, 5, Menu.NONE, "OFF");
    	mapMode.setGroupCheckable(2, true, true);
    	return true;
    } 
	
	
	public void goToMyLocation(){
		
	//	String context = Context.LOCATION_SERVICE;
	//	locationManager = (LocationManager)getSystemService(context);
	//	Double latitude = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1E6;
	//	Double longitude = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1E6;
		
		
		
		//mapView.getController().zoomToSpan(10000, 10);
	//	locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		Intent intent = getIntent();
		
		Double longitude = intent.getDoubleExtra("longitude", 0d)*1E6;
		Double latitude = intent.getDoubleExtra("latitude", 0d)*1E6;
		
		GeoPoint point1= new GeoPoint(latitude.intValue(),longitude.intValue());
		
		Site site = (Site)intent.getSerializableExtra("site");
		GeoPoint point2 = new GeoPoint(((Double)(site.getSiteLatitude()*1E6)).intValue(),((Double)(site.getSiteLongitude()*1E6)).intValue());
		MapOverlayPoints overlay= new MapOverlayPoints(point1,point2,site.getSiteName());
		mapView.getController().animateTo(new GeoPoint((point1.getLatitudeE6()+point2.getLatitudeE6())/2,(point1.getLongitudeE6()+point2.getLongitudeE6())/2));
				
        Location.distanceBetween(point1.getLatitudeE6()/1E6, point1.getLongitudeE6()/1E6,point2.getLatitudeE6()/1E6, point2.getLongitudeE6()/1E6, results);
        if(results.length>1){
        Log.i("Rezultati",String.valueOf(results[0])+" "+String.valueOf(results[1]));
        }
        else{
        	Log.i("Rezultati:",String.valueOf(results[0]));
        }
        
       
        compassOverlay.enableCompass();
        
        mapView.getController().zoomToSpan(Math.abs(point1.getLatitudeE6()-point2.getLatitudeE6()), Math.abs(point1.getLongitudeE6()-point2.getLongitudeE6()));
        
        listOfOverlays.add(overlay);
        listOfOverlays.add(compassOverlay);
        
        mapView.postInvalidate();
	}
	/** Called when we select item from Menu.
	 * 
	 */
	public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch (item.getItemId()) {
       	case 1:{
       		mapView.setStreetView(true);
       		mapView.setSatellite(false);
       		mapView.setTraffic(false);
       		return true;
       	}
       	case 2:{

       		mapView.setStreetView(false);
       		mapView.setSatellite(true);
       		mapView.setTraffic(false);
       		return true;
       	}
       	case 3:{
       		mapView.setStreetView(false);
       		mapView.setSatellite(false);
       		mapView.setTraffic(true);
       		return true;
       	}
       	case 4:{
       		compassOverlay.enableCompass();
       		listOfOverlays.add(compassOverlay);
       		return true;
       	}
       	case 5:{
       		compassOverlay.disableCompass();
       		listOfOverlays.remove(compassOverlay);
       		return true;
       	}
    	}
    	return true;
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// IMPORTANT: This method must return true if your Activity
		// is displaying driving directions. Otherwise return false.
		return false;
	}

	public MapPreviewActivity  pointer()
	{
		return this;
	}	
	     
}	

